﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestSequence.Question;
namespace LongestSequence.Answer
{
    class Effie : ILongestSequence
    {
        public int GetLongestSequence(int[] array)
        {
            int i = 0;
            int j = 0;
            int m = 0;
            int Delta = 0;
            int Zero = 0;
            int count = 0;
            for (i = 0; i < array.Length; i++)
            {
                for (j = i; j < array.Length; j++)
                {
                    int len = j - i + 1;
                    int[] TempArray = new int[len];
                    int[] SortedArray = new int[len];
                    Array.ConstrainedCopy(array, i, TempArray, 0, len);
                    sort(TempArray, out SortedArray);
                    Zero = 0;
                    Delta = 0;
                    if (SortedArray.Length == 1 || (SortedArray[0] == 0 && (SortedArray[len - 1] == 0)))
                    {
                        count = Math.Max(SortedArray.Length, count);
                    }
                    else
                    {
                        if (SortedArray[0] == 0)
                        {
                            for (m = 0; m < SortedArray.Length; m++)
                            {

                                if (SortedArray[m] == 0)
                                {
                                    Zero = Zero + 1;
                                }

                            }
                            Delta = SortedArray[SortedArray.Length - 1] - SortedArray[Zero] - 1;
                        }
                        else if (SortedArray[len - 1] == 0)
                        {
                            for (m = len - 1; m > 0; m--)
                            {
                                if (SortedArray[m] == 0)
                                {
                                    Zero = Zero + 1;
                                }

                            }
                            Delta = SortedArray[SortedArray.Length - Zero - 1] - SortedArray[0] - 1;
                        }
                        else
                            Delta = SortedArray[SortedArray.Length - 1] - SortedArray[Zero] - 1;

                        if (Delta <= SortedArray.Length - 2)
                        {
                            count = Math.Max(SortedArray.Length, count);
                        }
                    }
                }
            }
            return count;

        }
        public void sort(int[] myArray, out int[] SortedArray)
        {
            SortedArray = new int[myArray.Length];
            for (int i = 0; i < myArray.Length - 1; i++)
            {
                for (int j = 0; j < myArray.Length - i - 1; j++)
                {

                    if (myArray[j] > myArray[j + 1])
                    {
                        int temp = myArray[j];
                        myArray[j] = myArray[j + 1];
                        myArray[j + 1] = temp;
                    }
                }

            }
            for (int i = 0; i < myArray.Length; i++)
            {
                SortedArray[i] = myArray[i];
            }
        }
    }
}
